package com.example.springboot.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description: 设置接口统一前缀
 *
 * @Author 马钰涵
 * @Create 2023/8/6 18:18
 * @Version 1.0
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer){
        //指定controller统一接口前缀，找到加有RestController注解的类，统一添加前缀：/api
        configurer.addPathPrefix("/api", clazz-> clazz.isAnnotationPresent(RestController.class));
    }

    //token拦截器规则
    //.excludePathPatterns("/api/admin/login")放开登录接口，因为登陆的时候还没有token
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/api/**").excludePathPatterns("/api/admin/login");
    }
}
